package com.group11.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.group11.resume.VO.ResumeVO;
import com.group11.resume.constants.ResumeConstants;
import com.group11.resume.entity.Resume;
import com.group11.resume.mapper.ResumeMapper;
import com.group11.resume.service.EduBgService;
import com.group11.resume.service.ResumeService;
import com.group11.resume.service.WorkExpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ResumeServiceImpl implements ResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private EduBgService eduBgService;
    @Autowired
    private WorkExpService workExpService;
    public void addResume(Resume resume) {
        resume.setRefreshTime(LocalDateTime.now());
        resume.setViewNum(0);
        resume.setStatus(ResumeConstants.Status.ACTIVE);
        resume.setIsDeleted(ResumeConstants.DeleteFlag.NOT_DELETED);
        resume.setCreatedTime(LocalDateTime.now());
        resumeMapper.insert(resume);
    }

    public void createResume(Integer userId, ResumeVO resumeVO) {
        addResume(resumeVO.getResume());
        Integer resumeId = resumeVO.getResume().getId();
        setDefaultResume(userId, resumeId);
        resumeVO.getEduBgs().forEach(eduBg -> eduBg.setResumeId(resumeId));
        resumeVO.getWorkExps().forEach(workExp -> workExp.setResumeId(resumeId));
        eduBgService.addEduBg(resumeVO.getEduBgs());
        workExpService.addWorkExp(resumeVO.getWorkExps());
    }

    public void updateResume(Resume resume) {
        resumeMapper.updateById(resume);
    }

    public List<Resume> listByUserId(Integer userId) {
        return resumeMapper.selectList(new QueryWrapper<Resume>()
                .eq("user_id", userId)
                .eq("is_deleted", ResumeConstants.DeleteFlag.NOT_DELETED));
    }

    //查找简历详情，需要同时拿到对应的教育经历和工作经历
    public ResumeVO getById(Integer id) {
        ResumeVO resumeVO = new ResumeVO();
        resumeVO.setResume(resumeMapper.selectById(id));
        resumeVO.setEduBgs(eduBgService.listByResumeId(id));
        resumeVO.setWorkExps(workExpService.listByResumeId(id));
        return resumeVO;
    }

    public void refreshResume(Integer id) {
        //todo: 刷新简历需要处理的业务
        resumeMapper.update(new UpdateWrapper<Resume>()
                .set("refresh_time", LocalDateTime.now())
                .eq("id", id));
    }

    public void deleteResumes(Integer userId,List<Integer> ids) {
        resumeMapper.update(new UpdateWrapper<Resume>()
                .set("is_deleted", ResumeConstants.DeleteFlag.DELETED)
                .in("id", ids));
        eduBgService.deleteEduBg(ids);
        workExpService.deleteWorkExp(ids);
        Integer maxResumeId = (Integer)resumeMapper.selectObjs(new QueryWrapper<Resume>()
                .select("id")
                .eq("user_id",userId)
                .eq("is_deleted",ResumeConstants.DeleteFlag.NOT_DELETED)
                .orderByDesc("view_num")
                .last("LIMIT 1"))
                .stream()
                .findFirst()
                .orElse(null);
        setDefaultResume(userId,maxResumeId);
    }

    public void setDefaultResume(Integer userId, Integer resumeId) {
        resumeMapper.update(new UpdateWrapper<Resume>()
                .set("def", ResumeConstants.DefFlag.DEFAULT)
                .eq("id", resumeId));
        resumeMapper.update(new UpdateWrapper<Resume>()
                .set("def", ResumeConstants.DefFlag.NOT_DEFAULT)
                .eq("user_id", userId)
                .ne("id", resumeId));
    }
}
